
* This dofile is for preparing the CSES_IMD data


clear all
	
cd "${data}"	
		
use cses_imd.dta, clear
	

gen swd = .
replace swd = 0 if IMD3010== 5
replace swd = 1 if IMD3010== 4
replace swd = 2 if IMD3010==6
replace swd = 3 if IMD3010==2
replace swd = 4 if IMD3010==1


gen efficacy = .	// who is in power makes a difference 
replace efficacy = 0 if IMD3011==1
replace efficacy = 1 if IMD3011==2
replace efficacy = 2 if IMD3011==3
replace efficacy = 3 if IMD3011==4
replace efficacy = 4 if IMD3011==5


gen swg = .
replace swg = 0 if IMD3014==4
replace swg = 1 if IMD3014==3
replace swg = 2 if IMD3014==2
replace swg = 3 if IMD3014==1


gen represent = .
replace represent = 0 if IMD3016_1==0
replace represent = 1 if IMD3016_1==1


gen daysafter = IMD1014_1 if IMD1014_1<363

rename IMD5000_*? IMD5000_#, renumber	// party identifier
recode IMD5000_* (9999999=.)

rename IMD5001_*? IMD5001_#, renumber // percent vote 
recode IMD5001_* (999=.) (997=.)

gen perception_dk_1 = IMD3007_A
gen perception_dk_2 = IMD3007_B
gen perception_dk_3 = IMD3007_C
gen perception_dk_4 = IMD3007_D
gen perception_dk_5 = IMD3007_E
gen perception_dk_6 = IMD3007_F
gen perception_dk_7 = IMD3007_G
gen perception_dk_8 = IMD3007_H
gen perception_dk_9 = IMD3007_I

rename IMD5011_*? IMD5011_#, renumber 
recode IMD5011_* (90/99=.)

rename IMD3007_*? IMD3007_#, renumber	// party placement by voters
recode IMD3007_* (95/99=.)

rename IMD5012_*? IMD5012_#, renumber // expert placement of parties
recode IMD5012_* (95/99=.)

rename IMD5031_*? IMD5031_#, renumber // government composition
rename IMD5001_*? IMD5001_#, renumber // vote share 

rename IMD5102_*? IMD5102_#, renumber // ches identifier 
recode IMD5102_* (9999=.)

rename IMD1006_NAM country 
rename IMD1004 election 

rename IMD5100_*? IMD5100_#, renumber // CMP 
recode IMD5100_* (999999=.)

forval i = 1/9 {
	replace IMD5001_`i' = . if IMD5001_`i' == 999
	replace IMD5001_`i' = . if IMD5001_`i' == 997
}


gen mailback = .
replace mailback = 1 if IMD1016_1==3 | IMD1016_2==3 // mail or self-completed 


keep daysafter IMD1005 IMD3007_* IMD5012_* country  election  IMD3006 IMD5031_* IMD5000_* IMD5001_* IMD3001 IMD3010 perception_dk_* mailback IMD2007 IMD2006 IMD2003 IMD2002 IMD2001_1 IMD5102_* IMD1008_YEAR IMD5100_* IMD5011_* IMD5001_* swd efficacy swg represent IMD2002 IMD2014 IMD3005_1

gen respondent = _n
drop IMD1005 

reshape long IMD3007_ IMD5012_ IMD5000_ IMD5031_ IMD5001_ perception_dk_ IMD5102_ IMD5100_ IMD5011_, i(respondent) j(partyorder)

drop if daysafter==.
drop if perception_dk_ == 95 | perception_dk_ == 96 | perception_dk_ == 97 | perception_dk_ == 99
drop if respondent==.


*We focus on established democracies:

keep if country=="Australia" | country=="Austria" | country=="Belgium" | country=="Canada" | country=="Denmark" | country=="Finland" | country=="France" | country=="Germany" | country=="Greece" | country=="Iceland" | country=="Ireland" | country=="Italy" | country=="Japan" | country=="Netherlands" | country=="New Zealand" | country=="Norway" | country=="Portugal" | country=="Spain" | country=="Sweden" | country=="Switzerland" | country=="Great Britain" | country=="United States of America" 


*These are problematic surveys that we identified:

drop if election=="DNK_2001" | election=="FIN_2003" | election=="FRA_2002" | election=="GRC_2012" | election=="ISL_2007" | election=="ITA_2006" | election=="JPN_2004" | election=="NOR_2001" | election=="PRT_2005"
 
drop if country=="Japan"


* Code government status 
gen ingovernment=.	// if the party has more than 0 portfolio, it is in the government 
replace ingovernment = 1 if IMD5031_>0 & IMD5031_!=.
replace ingovernment = 0 if IMD5031_==0

save "d1.dta", replace 



